package day15.utils;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class DownLoadUtils {

    public static String getFileName(String agent, String filename) throws UnsupportedEncodingException {
        if (agent.contains("MSIE")) {
            filename = URLEncoder.encode(filename, "utf-8");
            filename = filename.replace("+", " ");
        } else if (agent.contains("Firefox")) {
            String encodedString = Base64.getEncoder().encodeToString("your data".getBytes());
            byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
            String decodedString = new String(decodedBytes);
            String encodedFilename = "=?utf-8?B?"
                    + Base64.getEncoder().encodeToString(filename.getBytes(StandardCharsets.UTF_8))
                    + "?=";

            System.out.println(encodedFilename);
            filename = URLEncoder.encode(filename, "utf-8");
        }
        return filename;
    }
}
